package cn.ava.org.controller;

import cn.ava.basic.exception.BusinessException;
import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.ExcelUtil;
import cn.ava.basic.utils.PageList;
import cn.ava.org.domain.Shop;
import cn.ava.org.domain.ShopAuditLog;
import cn.ava.org.query.ShopQuery;
import cn.ava.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ShopController
 * @Description: 店铺控制层接口
 * @Author: Avavaava
 * @Date: 2022/6/24 15:07
 * @version: 23.53
 *
 */
@Api(value = "店铺Controller层",description = "店铺列表CRUD模块")
@RestController
@RequestMapping("/shop")
public class ShopController {

    // 实例化接口对象
    @Autowired
    private IShopService shopService;

    /**
    * @Title: list
    * @Description: 查询所有部门列表
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:27
    * @param:
    * @return:
    **/
    @GetMapping
    @ApiOperation(value = "查询所有部门")
    public List<Shop> list() {
        return shopService.loadAll();
    }

    /**
    * @Title: getById
    * @Description: 根据部门id查询部门
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:28
    * @param:
    * @return:
    **/
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询部门数据")
    public Shop getById(@PathVariable("id") Long id) {
        return shopService.getById(id);
    }

    /**
    * @Title: delete
    * @Description: 根据部门id删除部门列表数据
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:28
    * @param:
    * @return:
    **/
    @DeleteMapping("/{id}")
    public AjaxResult delete(@ApiParam(value = "主键",readOnly = true) @PathVariable("id") Long id) {
        try {
            shopService.delete(id);
           return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增或修改部门数据
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:29
    * @param:
    * @return:
    **/
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
    * @Title: queryDept
    * @Description: 高级查询部门列表
    * @Author: Avavaava
    * @Version: 23.53
    * @Date:  2022/6/24 16:29
    * @param:
    * @return:
    **/
    @PostMapping
    public PageList<Shop> queryEmp(@RequestBody ShopQuery shopQuery){
        return shopService.queryData(shopQuery);
    }

    /**
     * @Title: settlement
     * @Description: 商铺入驻,管理员进行管理判断
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/3 9:27
     * @param:
     * @return:
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {

        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
           return new AjaxResult().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: pass
     * @Description: 店铺人工审核发送通过邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/3 9:27
     * @param:
     * @return:
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: reject
     * @Description: 店铺人工审核发送驳回邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/3 9:27
     * @param:
     * @return:
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: Repulse
     * @Description: 店铺人工审核发送拒绝邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/3 9:26
     * @param:
     * @return:
     */
    @PostMapping("/audit/Repulse")
    public AjaxResult Repulse(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.Repulse(shopAuditLog);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }


    /**
     * @Title: exportExcel
     * @Description: 导出Excel文件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/2 19:31
     * @param:
     * @return:
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response) {
        // 查出所有数据
        List<Shop> shops = shopService.loadAll();
        // 使用Excel工具类进行导出
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }

    /**
     * @Title: exportExcel
     * @Description: 导入Excel文件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/2 19:31
     * @param:
     * @return:
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart MultipartFile file) {
        // 使用Excel工具类进行导入
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.batchAdd(shops);
    }


    /**
     * @Title: activateShop
     * @Description:  激活店铺邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/8 19:34
     * @param:
     * @return:
     */
    @PostMapping("/audit/activateShop/{id}")
    public AjaxResult activateShop(@PathVariable("id") Long id) {
        try {
            shopService.activateShop(id);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: amendShop
     * @Description: 修改店铺邮件
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/8 19:34
     * @param:
     * @return:
     */
    @PostMapping("/audit/amendShop")
    public AjaxResult amendShop(@RequestBody Shop shop) {
        try {
            shopService.amendShop(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    @GetMapping("/audit/{id}")
    public Shop auditGetById(@PathVariable("id") Long id) {
        return shopService.getById(id);
    }



    /**
     * @Title: ShopEcharts
     * @Description: 店铺报表数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/8 17:29
     * @param:
     * @return:
     */
    @GetMapping("/echarts")
    public Map<String,Object> ShopEcharts() {
        return shopService.getShopEcharts();
    }


    /**
     * @Title: batchDelete
     * @Description: 批量删除数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/25 11:19
     * @param:
     * @return:
     */
    @PatchMapping
    public AjaxResult batchDelete( @RequestBody Long[] id) {
        try {
            shopService.batchDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }


}
